﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;
using WXYW.Model;

namespace WXYW
{
    public partial class SFTJBaseOnSFTJ : System.Web.UI.Page
    {
        public string TotalString { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialDropDown();
                InitialTotalText();
            }
        }

        private void InitialTotalText()
        {
            totalYearLabel.Text = totalYearDropDownList.SelectedValue;
            ApplicationProductService productService = new ApplicationProductService();
            GatheringService gservice = new GatheringService();
            //
            IQueryable<ApplicationProduct> products = productService.GetProductByGatheringYear(Convert.ToInt32(totalYearDropDownList.SelectedValue));
            totalLabel.Text = products.Sum(p => p.Cost).ToString();
            shouldLabel.Text = totalLabel.Text;
            //
            IQueryable<WXYW.DAL.Gathering> gatherings = gservice.GetGatheringsByYear(Convert.ToInt32(totalYearDropDownList.SelectedValue));
            resultLabel.Text = gatherings.Sum(g => g.RealPay).ToString();
        }

        private void InitialDropDown()
        {
            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                totalYearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            totalYearDropDownList.SelectedIndex = totalYearDropDownList.Items.Count - 1;

            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                tyearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            tyearDropDownList.SelectedIndex = tyearDropDownList.Items.Count - 1;

            for (int i = 1; i <= 12; i++)
            {
                monthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            for (int i = 1; i <= 12; i++)
            {
                tmonthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            tmonthDropDownList.SelectedIndex = DateTime.Now.Month - 1;

            ServiceStationService service = new ServiceStationService();
            List<ServiceStation> stations = service.GetALLServiceStations().ToList();
            stations.Insert(0, new ServiceStation { ID = 0, Name = "所有平台" });
            serviceStationDropDownList.DataSource = stations;
            serviceStationDropDownList.DataTextField = "Name";
            serviceStationDropDownList.DataValueField = "ID";
            serviceStationDropDownList.DataBind();
            IntialText();
        }

        private void IntialText()
        {
            yearLabel.Text = yearDropDownList.SelectedValue;
            monthLabel.Text = monthDropDownList.SelectedValue;
            tyearLabel.Text = tyearDropDownList.SelectedValue;
            tmonthLabel.Text = tmonthDropDownList.SelectedValue;
            serviceLabel.Text = serviceStationDropDownList.SelectedItem.Text;
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            IntialText();
            GatheringService service = new GatheringService();
            List<SFTJModel> sftjModels = service.GetSFTJModelBy(Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(monthDropDownList.SelectedValue), Convert.ToInt32(tyearDropDownList.SelectedValue), Convert.ToInt32(tmonthDropDownList.SelectedValue), Convert.ToInt32(serviceStationDropDownList.SelectedValue));
            formsRepeater.DataSource = sftjModels;
            formsRepeater.DataBind();

            TotalString = string.Format("<td>{0}</td><td>{1}</td><td>{2}</td><td>￥{3}元</td>", sftjModels.Sum(s => s.TF), sftjModels.Sum(s => s.BXPX), sftjModels.Sum(s => s.BWBA), sftjModels.Sum(s => s.ZCWX));
        }

        protected void totalYearDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitialTotalText();
        }

    }
}
